using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
using AdminSystem.API.DTOs;
using AdminSystem.API.Services;

namespace AdminSystem.API.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    [Authorize]
    public class ContentsController : ControllerBase
    {
        private readonly IContentService _contentService;

        public ContentsController(IContentService contentService)
        {
            _contentService = contentService;
        }

        [HttpGet]
        public async Task<ActionResult<List<ContentDto>>> GetAllContents()
        {
            var contents = await _contentService.GetAllContentsAsync();
            return Ok(contents);
        }

        [HttpGet("{id}")]
        public async Task<ActionResult<ContentDto>> GetContent(int id)
        {
            var content = await _contentService.GetContentByIdAsync(id);
            if (content == null)
            {
                return NotFound();
            }
            return Ok(content);
        }

        [HttpPost]
        public async Task<ActionResult<ContentDto>> CreateContent([FromBody] CreateContentDto createContentDto)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            try
            {
                var authorId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? "0");
                var content = await _contentService.CreateContentAsync(createContentDto, authorId);
                return CreatedAtAction(nameof(GetContent), new { id = content.Id }, content);
            }
            catch (Exception ex)
            {
                return BadRequest(new { message = ex.Message });
            }
        }

        [HttpPut("{id}")]
        public async Task<ActionResult<ContentDto>> UpdateContent(int id, [FromBody] UpdateContentDto updateContentDto)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            try
            {
                var content = await _contentService.UpdateContentAsync(id, updateContentDto);
                if (content == null)
                {
                    return NotFound();
                }
                return Ok(content);
            }
            catch (Exception ex)
            {
                return BadRequest(new { message = ex.Message });
            }
        }

        [HttpDelete("{id}")]
        public async Task<ActionResult> DeleteContent(int id)
        {
            var result = await _contentService.DeleteContentAsync(id);
            if (!result)
            {
                return NotFound();
            }
            return NoContent();
        }

        [HttpPost("{id}/publish")]
        public async Task<ActionResult> PublishContent(int id)
        {
            var result = await _contentService.PublishContentAsync(id);
            if (!result)
            {
                return NotFound();
            }
            return Ok(new { message = "Content published successfully" });
        }

        [HttpPost("{id}/unpublish")]
        public async Task<ActionResult> UnpublishContent(int id)
        {
            var result = await _contentService.UnpublishContentAsync(id);
            if (!result)
            {
                return NotFound();
            }
            return Ok(new { message = "Content unpublished successfully" });
        }
    }
}
